<template>
  <div>
    <search :formItems='searchItems' @search='getList' ref='getListSearch' />
    <el-button type="primary" class="marginBottom10" @click="isAdd = true; showAddBox = true; cleanForm();resetForm('getListForm');">新增榜单分配</el-button>
    <el-table :data='tableData' style='width: 100%'>
      <el-table-column type="index" label='序号' ></el-table-column>
      <el-table-column prop='createtime' sortable label='创建时间' ></el-table-column>
      <el-table-column prop='type' label='榜单类型' >
        <template slot-scope="scope">
          <!-- 【1-最受欢迎榜；2-最活跃榜】 -->
          <span v-if="scope.row.type === '1'">最受欢迎榜</span>
          <span v-if="scope.row.type === '2'">最活跃榜</span>
        </template>
      </el-table-column>
      <el-table-column prop='datetype' label='时间维度'>
        <template slot-scope="scope">
          <!-- 时间类型【1-周；2-月；3-年；4-总】 -->
          <span v-if="scope.row.datetype === '1'">周</span>
          <span v-if="scope.row.datetype === '2'">月</span>
          <span v-if="scope.row.datetype === '3'">年</span>
          <span v-if="scope.row.datetype === '4'">总</span>
        </template>
      </el-table-column>
      <el-table-column prop='startplace' label='开始名次'></el-table-column>
      <el-table-column prop='endplace' label='结束名次'></el-table-column>
      <el-table-column prop='luckdrawcount' label='获得抽奖次数'></el-table-column>
      <el-table-column prop='msg' label='通知文案'></el-table-column>
      <el-table-column label='操作'>
        <template slot-scope="scope">
          <span class="astyle"  @click="editItem(scope.row)">编辑</span>
          <span class="dstyle"  @click="deleteItem(1, scope.row.id)">删除</span>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.page"
        :page-sizes="[20, 50, 100]"
        :page-size="page.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
    <!-- 表单start -->
    <el-dialog
      :close-on-click-modal="false"
      :title="isAdd ? '新增' : '编辑'"
      :visible.sync="showAddBox"
      >
      <el-form :model="diaForm" :rules="rules" ref="getListForm" label-width="110px">

        <el-form-item label="榜单类型" prop="type">
          <el-radio-group v-model="diaForm.type">
            <el-radio label="1">最受欢迎榜</el-radio>
            <el-radio label="2">最活跃榜</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="时间维度" prop="datetype">
          <el-radio-group v-model="diaForm.datetype">
            <el-radio label="4">总</el-radio>
            <el-radio label="3">年</el-radio>
            <el-radio label="2">月</el-radio>
            <el-radio label="1">周</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="开始名次" prop="startplace">
          <el-input v-model.number="diaForm.startplace"></el-input>
        </el-form-item>

        <el-form-item label="结束名次" prop="endplace">
          <el-input v-model.number="diaForm.endplace"></el-input>
        </el-form-item>

        <el-form-item label="获得抽奖次数" prop="luckdrawcount">
          <el-input v-model.number="diaForm.luckdrawcount"></el-input>
        </el-form-item>

        <el-form-item label="通知文案" prop="msg">
          <el-input type="textarea" rows="3" v-model="diaForm.msg"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showAddBox = false">取 消</el-button>
        <el-button type="primary" @click="handleClickSendForm('getListForm')">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 表单end -->
  </div>
</template>
<script>
export default {
  name: 'GetList',
  data () {
    return {
      isAdd: false,
      showAddBox: false,
      diaForm: {
        id: '',
        type: '',
        datetype: '',
        startplace: '',
        endplace: '',
        msg: '',
        luckdrawcount: ''
      },
      tableData: [],
      searchItems: [
        {
          label: '榜单类型',
          type: 'select',
          key: 'type',
          options: [{ id: '1', name: '最受欢迎榜' }, { id: '2', name: '最活跃榜' }]
        },
        {
          label: '时间维度',
          type: 'select',
          key: 'datetype',
          options: [{ id: '1', name: '周' }, { id: '2', name: '月' }, { id: '3', name: '年' }, { id: '4', name: '总' }]
        },
        {
          label: '获得抽奖次数',
          width: '100',
          type: 'input',
          key: 'luckdrawcount'
        }
      ],
      page: {
        page: 1,
        size: 20,
        total: 1
      },
      rules: {
        type: [
          { required: true, message: '请选择榜单类型', trigger: 'blur' }
        ],
        datetype: [
          { required: true, message: '请选择时间维度', trigger: 'blur' }
        ],
        startplace: [
          { required: true, message: '开始名次不能为空' },
          { type: 'number', message: '必须为数字值' }
        ],
        endplace: [
          { required: true, message: '结束名次不能为空' },
          { type: 'number', message: '必须为数字值' }
        ],
        luckdrawcount: [
          { required: true, message: '获得抽奖次数不能为空' },
          { type: 'number', message: '必须为数字值' }
        ],
        msg: [
          { required: true, message: '通知文案不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    getList () {
      let searchData = {}
      if (this.$refs['getListSearch']) {
        searchData = this.$refs['getListSearch'].search()
      }
      let params = {
        ...searchData,
        page: this.page.page,
        size: this.page.size
      }
      this.axios.get('/Luckdraw/rewardindex', { params }).then(res => {
        if (res.data.status === 1 || res.data.status === '1') {
          this.page.total = Number(res.data.data.count)
          this.tableData = res.data.data.list
        }
      })
    },
    handleSizeChange (val) {
      this.page.size = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.page.page = val
      this.getList()
    },
    editItem (row) {
      let self = this
      for (let key in this.diaForm) {
        self.diaForm[key] = row[key]
      }
      self.diaForm['startplace'] = Number(row['startplace'])
      self.diaForm['endplace'] = Number(row['endplace'])
      self.diaForm['luckdrawcount'] = Number(row['luckdrawcount'])
      this.isAdd = false
      this.showAddBox = true
    },
    deleteItem (e = 1, val) {
      let self = this
      let params = {}
      params.id = val
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.axios.get('/Luckdraw/delreward', { params: {id: val} }).then(res => {
          if (res.data.status === 1 || res.data.status === '1') {
            self.$message({
              message: '删除成功',
              type: 'success'
            })
            self.getList()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    cleanForm () {
      let self = this
      for (let key in self.diaForm) {
        self.diaForm[key] = ''
      }
    },
    resetForm (formName) {
      try {
        this.$refs[formName].resetFields()
      } catch (e) {}
    },
    handleClickSendForm (formName) {
      let url = this.isAdd ? '/Luckdraw/addreward' : '/Luckdraw/editreward'
      let self = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = {}
          for (let key in self.diaForm) {
            if (key !== 'id') {
              if (self.diaForm[key] !== '') {
                params[key] = self.diaForm[key]
              }
            }
          }
          if (!self.isAdd) {
            params['id'] = self.diaForm.id
          }
          this.axios.post(url, self.qs.stringify(params)).then(res => {
            if (res.data.status === 1 || res.data.status === '1') {
              self.$message({
                message: '成功',
                type: 'success'
              })
              self.showAddBox = false
              self.getList()
            }
          })
        } else {
          return false
        }
      })
    }
  },
  mounted () {
    this.getList()
  }
}
</script>
<style scoped>
@import "../../../assets/css/style.css";
</style>
